// pages/share/detail/comments.js

const app = getApp();
import {
  circledynamicCommentList,
  circledynamicCommentAdd,
  ykCircledynamicCommentList,
  circledynamicCommentComment
} from '../../../utils/http'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    v: app.globalData.v, //版本
    lang: app.vSwitch(), //版本
    theme: app.cSwitch(), //主题色
    patterns: app.globalData.patterns, //是否为游客模式
    isIphoneX: app.globalData.isIphoneX,

    // 分页
    page: 1,
    size: 20,
    pages: 0,
    current: 0,
    total: 0,
    noData: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.init();
  },

  async init() {
    let t = this;
    t.setData({
      page: 1,
      pages: 0,
      current: 0,
      noData: false
    })
    t.getMore();
  },
  getMore() {
    let t = this;
    var promise = new Promise((resolve, reject) => {
      if (t.data.pages >= t.data.current) {
        let func = app.globalData.patterns && !app.globalData.token ? ykCircledynamicCommentList : circledynamicCommentList;
        func({
          page: t.data.page,
          pageSize: t.data.size,
          dynamic: t.data.id
        }).then(res => {
          if (res.httpCode == 200 || res.code == 0) {
            let r = res.data;
            let d;
            if (t.data.page == 1) {
              d = [];
            } else {
              d = t.data.d;
            }
            if (r.records.length) {
              r.records.forEach(i => {
                i.creatTime ? i.creatTime = i.creatTime.substring(0, i.creatTime.length - 3) : '';
                d.push(i)
              })
              if (r.records.length < r.size) {
                t.setData({
                  noData: true,
                })
              } else {
                t.setData({
                  noData: false,
                })
              }
            } else {
              t.setData({
                noData: true,
              })
            }
            t.setData({
              d,
              page: ++t.data.page,
              pages: r.pages,
              current: r.current,
              total: r.total
            })
          }

          resolve();
        })
      } else {
        t.setData({
          noData: true
        })
        resolve();
      }
    });
    return promise;
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      theme: app.cSwitch(), //主题色
      patterns: app.globalData.patterns, //是否为游客模式
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: this.data.theme.tabbarColor,
    })
  },

  fabu() {
    let t = this;
    let func;
    if (
      trim(t.data.content)) {
      if (t.data.people) {
        let str = '@' + t.data.people.name + '：';
        let content = trim(t.data.content);
        if (content.indexOf(str) != -1) {
          content = content.replace(str, '');
          if (trim(content)) {
            func = circledynamicCommentComment({
              dynamic: t.data.id,
              contentUser: t.data.people.id,
              content: trim(content)
            })
          } else {
            wx.showToast({
              title: '请输入评论内容',
              icon: 'none'
            })
            return false;
          }

        } else {
          func = circledynamicCommentAdd({
            dynamic: t.data.id,
            content: trim(t.data.content)
          })
        }

      } else {
        func = circledynamicCommentAdd({
          dynamic: t.data.id,
          content: trim(t.data.content)
        })
      }

      func.then(r => {
        if (r.code == 0) {
          if (r.data == 0) {
            wx.showToast({
              title: '提交成功,请耐心等待管理员审核',
              icon: 'none',
              mask: true
            })
          } else {
            wx.showToast({
              title: '发布成功',
              icon: 'none',
              mask: true
            })
          }

          setTimeout(() => {
            this.init();
            t.setData({
              content: ''
            })
          }, 1500)
        }
      })
    } else {
      wx.showToast({
        title: '请输入评论内容',
        icon: 'none'
      })
    }
  },

  huifu(e) {
    let t = this;
    console.log(e.mark);
    if (!this.data.people || e.mark.id != this.data.people.id) {
      this.setData({
        content: '@' + e.mark.name + '：',
      })
    } else if (this.data.people && e.mark.id == this.data.people.id) {
      let str = '@' + t.data.people.name + '：';
      let content = trim(t.data.content);
      if (content.indexOf(str) == -1) {
        this.setData({
          content: '@' + e.mark.name + '：',
        })
      }
    }

    this.setData({
      people: {
        id: e.mark.id,
        name: e.mark.name,
      },
      focus: true
    })
  },


  bindkeyboardheightchange(e) {
    console.log(e)
    this.setData({
      bottom: e.detail.height
    })
  },

  bindblur(e) {
    // console.log(e)
    this.setData({
      bottom: 0
    })
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.init();

    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000)

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.noData) {
      this.getMore();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.share();
  }
})

function trim(str) { //删除左右两端的空格
  return str.replace(/(^\s*)|(\s*$)/g, "");
}